/**
 * Created by jaliu on 2015/2/13.
 */

var adminUserService = require('../../services/backoffice/backOffice-adminUserService');
var messageUtil = require('../../common/utils/messageUtil');
var toolUtil = require('../../common/utils/toolUtil');


exports.adminUserLogin = function (req, res) {
    var input = req.body;
    adminUserService.adminUserLoginService(input, function (err, user) {
        if (err) {
            logger.error('Exception happened when login ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when login ...!' + err.message));
        } else {
            if (user != null) {
                logger.debug("Succeeded to login! - " + user._id);

                //req.session.backOfficeUser = user[0];

                var token = toolUtil.generateAndCacheToken(user._id);
                res.json(messageUtil.composeSucceededMessage(req, null, user));
            } else {
                logger.debug("Failed to login!....");
                res.json(messageUtil.composeFailedMessage(req, "Invalid userId and passord!..."));
            }
        }
    })
};

exports.adminUserCreate = function (req, res) {
    var input = req.body;
    adminUserService.adminUserCreateService(input, function (err, result) {
        if (err) {
            logger.error("Exception happened when trying to create backend adminUser..." + err);
            res.json(messageUtil.composeFailedMessage(req, "Exception happened when trying to create backend adminUser..." + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    });
};

exports.adminUserUpdate = function (req, res) {
    var loginId = req.params.loginId;
    var input = req.body;

    adminUserService.adminUserUpdateService(loginId, input, function (err, results) {
        if (err) {
            logger.error('Exception happened when trying to update backend User...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when trying to update backend User...!'));
        } else {
            if (results != 0) {
                logger.debug("Update backend User Service  successfully...!");
                res.json(messageUtil.composeSucceededMessage(req, null, null));
            } else {
                logger.error('Failed to update backend User , it is not existing! -' + id);
                res.json(messageUtil.composeFailedMessage(req, 'Failed to update backend User , it is not existing! -' + id));
            }
        }
    })
};

exports.adminUserDelete = function (req, res) {
    var loginId = req.params.loginId;
    adminUserService.adminUserDeleteService(loginId, function (err, results) {
        if (err) {
            logger.error('Exception happened when trying to delete backend User...!');
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when trying to delete backend User...!'));
        } else {
            if (results != null) {
                logger.debug("Delete backend User successfully...!");
                res.json(messageUtil.composeSucceededMessage(req, null, null));
            } else {
                logger.error('Failed to delete the backend User, it is not existing! -' + id);
                res.json(messageUtil.composeFailedMessage(req, 'Failed to delete backend User, it is not existing! -' + id));
            }
        }
    })
};

exports.adminUserFindById = function (req, res) {
    var loginId = req.params.loginId;
    adminUserService.adminUserFindById(loginId, function (err, results) {
        if (err) {
            logger.error('Exception happened when get backendUser by loginId ...!' + token);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when get backendUser by loginId...!'));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }
    });
};

exports.adminUserList = function (req, res) {

    var startPage = req.params.startPage;
    var pageSize = req.params.pageSize;
    if (startPage == null)startPage = 0;
    if (pageSize == null)pageSize = 100;

    adminUserService.adminUserListService(startPage, pageSize, function (err, results) {
        if (err) {
            logger.error('Exception happened when get backend frontendUser List ...!' + token);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when  backend frontendUser List...!'));
        } else {
            tempResult = {
                'totalNum': results[0],
                'items': results[1]
            },
                res.json(messageUtil.composeSucceededMessage(req, null, tempResult));
        }
    });
};